package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.dao.PortGroupDao;
import com.bocloud.cmp.entity.resource.PortGroup;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

@Service("portGroupDao")
public class PortGroupDaoImpl extends JdbcGenericDao<PortGroup, Long> implements PortGroupDao {

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from port_group a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<PortGroup> alarms = this.list(PortGroup.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (PortGroup port_group : alarms) {
			beans.add(new SimpleBean(port_group.getId(), port_group.getName()));
		}
		return beans;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from port_group a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update port_group set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public Boolean remove(Long id, Long userId) throws Exception {
		String sql = "update port_group set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public PortGroup query(Long id) throws Exception {
		String sql = "select a.*,b.name vendor_name from port_group a left join cloud_vendor b on a.vendor_id = b.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<PortGroup> list = this.list(PortGroup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public boolean removeByNetwork(Long networkId, Long userId) throws Exception {
		String sql = "update port_group set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where network_id = :networkId";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<PortGroup> listByNetwork(Long networkId) throws Exception {
		String sql = "select a.* from port_group a where a.is_deleted = 0 and a.network_id = :networkId";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		return this.list(PortGroup.class, sql, params);
	}
}
